package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.TeacherDetail;
import com.baidumap.administrator.happystudy.utils.GlideUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;

/**
 * 老师详情
 */
public class TeacherDetailActivity extends AppBaseActivity implements OnMessageResponse {


    @Bind(R.id.teacher_img)
    ImageView teacherImg;
    @Bind(R.id.tv_teacher_name)
    TextView tvTeacherName;
    @Bind(R.id.tv_teacher_section)
    TextView tvTeacherSection;
    @Bind(R.id.tv_grade)
    TextView tvGrade;
    @Bind(R.id.tv_teacher_rank)
    TextView tvTeacherRank;
    @Bind(R.id.tv_teacher_questions)
    TextView tvTeacherQuestions;

    @Bind(R.id.sdv_botom)
    SimpleDraweeView img_bottom;
    DraweeController draweeController;

    private String mTeacherId;

    @Override
    public int bindLayout() {
        return R.layout.activity_teacher_detail;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();
        Fresco.initialize(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.teacher_info);

        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.teacherDetail(this, mTeacherId);
    }

    /**
     * 获取bundleValue
     */
    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            mTeacherId = bundle.getString("teacher_id");
        }
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {

        if (url.equals(Constant.TEACHER_DETAIL) && jo != null) {
            TeacherDetail teacherDetail = new TeacherDetail(jo.toString());

            if (teacherDetail != null) {

                if (teacherDetail.getCode().equals("200")) {

                    GlideUtil.displayTeacherImg(this, Constant.DOMAIN + teacherDetail.getData().getTeacher_pic(), teacherImg);

                    tvTeacherName.setText(teacherDetail.getData().getName());
                    tvTeacherSection.setText(teacherDetail.getData().getSection());
                    tvGrade.setText(teacherDetail.getData().getGrade());
                    tvTeacherRank.setText(teacherDetail.getData().getRank());
                    if (teacherDetail.getData().getQuestions() != null) {

                        tvTeacherQuestions.setText(teacherDetail.getData().getQuestions() + "道");
                    } else {
                        tvTeacherQuestions.setText("0道");

                    }

                    draweeController = Fresco.newDraweeControllerBuilder()
                            .setAutoPlayAnimations(true)//自动播放动画
                            .setUri(Uri.parse(teacherDetail.getData().getImage()))//路径
                            .build();
                    img_bottom.setController(draweeController);
                } else {
                    ToastUtil.showMessage(this, teacherDetail.getMsg());
                }
            }
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }

}
